﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Admin.aspx.cs" Inherits="SalePriceSystem.Page.Admin"
    MasterPageFile="~/MasterPage/Backend.Master" %>

<asp:Content ID="HeadContent" runat="server" ContentPlaceHolderID="HeadContent">
    <link rel="stylesheet" href="/plugins/slick.grid.css" type="text/css" />
    <style>
        input.text
        {
            margin-bottom: 12px;
            padding: 0.4em;
            width: 95%;
        }
        .slick-cell-checkboxsel
        {
            background: #f0f0f0;
            border-right-color: silver;
            border-right-style: solid;
        }
    </style>
    <script src="/lib/jquery.event.drag-2.0.min.js"></script>
    <script src="/plugins/slick.core.js"></script>
    <script src="/plugins/slick.checkboxselectcolumn.js"></script>
    <script src="/plugins/slick.autotooltips.js"></script>
    <script src="/plugins/slick.cellrangedecorator.js"></script>
    <script src="/plugins/slick.cellrangeselector.js"></script>
    <script src="/plugins/slick.cellcopymanager.js"></script>
    <script src="/plugins/slick.cellselectionmodel.js"></script>
    <script src="/plugins/slick.rowselectionmodel.js"></script>
    <script src="/plugins/slick.formatters.js"></script>
    <script src="/plugins/slick.editors.js"></script>
    <script src="/plugins/slick.grid.js"></script>
    <script src="/Scripts/Helper.js" type="text/javascript"></script>
    <script type="text/javascript">
        var helper = new Helper();
        var grid;
        var data = [];
        var columns = [
    { id: "UserName", name: "UserName", field: "UserName", width: 120, cssClass: "cell-title" },
    { id: "DomainName", name: "DomainName", field: "DomainName" },
    { id: "FirstName", name: "FirstName", field: "FirstName", width: 80, resizable: false },
    { id: "Lastname", name: "Lastname", field: "Lastname", minWidth: 60 },
    { id: "Email", name: "Email", field: "Email", minWidth: 60 },
    { id: "RoleID", name: "RoleID", field: "RoleID", sortable: false, width: 80, minWidth: 20, maxWidth: 80, cssClass: "cell-effort-driven" }
  ];
        var checkboxSelector = new Slick.CheckboxSelectColumn({
            cssClass: "slick-cell-checkboxsel"
        });
        columns.splice(0, 0, checkboxSelector.getColumnDefinition());

        var options = {
            editable: true,
            enableRowReordering: true,
            enableCellNavigation: true,
            asyncEditorLoading: false,
            autoEdit: false


        };
        function RefreshUser(parameters) {
            helper.GetUserDb(function (userdb) {
                grid = new Slick.Grid("#myGrid", userdb, columns, options);
                grid.setSelectionModel(new Slick.RowSelectionModel({ selectActiveRow: true }));
                grid.registerPlugin(checkboxSelector);
            });
        }
        $(function () {
            RefreshUser();

            //            for (var i = 0; i < 500; i++) {
            //                var d = (data[i] = {});

            //                d["title"] = "Task " + i;
            //                d["duration"] = "5 days";
            //                d["percentComplete"] = Math.min(100, Math.round(Math.random() * 110));
            //                d["start"] = "01/01/2009";
            //                d["finish"] = "01/05/2009";
            //                d["effortDriven"] = (i % 5 == 0);
            //            }

            //            grid = new Slick.Grid("#myGrid", data, columns, options);
        })
            
       
    </script>
    <script>

        $(function () {
            // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
            $("#dialog:ui-dialog").dialog("destroy");

            var name = $("#name"),
			email = $("#email"),
			password = $("#password"),
			allFields = $([]).add(name).add(email).add(password),
			tips = $(".validateTips");

            function updateTips(t) {
                tips
				.text(t)
				.addClass("ui-state-highlight");
                setTimeout(function () {
                    tips.removeClass("ui-state-highlight", 1500);
                }, 500);
            }

            function checkLength(o, n, min, max) {
                if (o.val().length > max || o.val().length < min) {
                    o.addClass("ui-state-error");
                    updateTips("Length of " + n + " must be between " +
					min + " and " + max + ".");
                    return false;
                } else {
                    return true;
                }
            }

            function checkRegexp(o, regexp, n) {
                if (!(regexp.test(o.val()))) {
                    o.addClass("ui-state-error");
                    updateTips(n);
                    return false;
                } else {
                    return true;
                }
            }

            function deleteUser(userName) {

                helper.DeleteUserDb(userName, function () {
                    alert('done');
                });
            }
            $("#dialog-form").dialog({
                autoOpen: false,
                height: 300,
                width: 350,
                modal: true,
                title: "Create an account",
                buttons: {
                    "Create an account": function () {
                        var bValid = true;
                        allFields.removeClass("ui-state-error");

                        bValid = bValid && checkLength(name, "username", 3, 16);
                        //                        bValid = bValid && checkLength(email, "email", 6, 80);
                        //                        bValid = bValid && checkLength(password, "password", 5, 16);

                        //                        bValid = bValid && checkRegexp(name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter.");
                        //                        // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
                        //                        bValid = bValid && checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com");
                        //                        bValid = bValid && checkRegexp(password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9");

                        if (bValid) {
                            helper.AddUserDb(name.val(), function () {
                                alert('Create user completed');
                                RefreshUser();
                                $(this).dialog("close");
                                //grid.updateRowCount();
                                //grid.render();
                            });
                            //                            $("#users tbody").append("<tr>" +
                            //							"<td>" + name.val() + "</td>" +
                            //							"<td>" + email.val() + "</td>" +
                            //							"<td>" + password.val() + "</td>" +
                            //						"</tr>");
                            //                            $(this).dialog("close");
                        }
                    },
                    Cancel: function () {
                        $(this).dialog("close");
                    }
                },
                close: function () {
                    allFields.val("").removeClass("ui-state-error");
                }
            });

            $("#create-user")
			.button()
			.click(function () {
			    $("#dialog-form").dialog("open");
			});
            $("#menuadd").click(function () {
                // grid.setSelectedRows(1);
                //grid.render();
                var selectedIndexes = grid.getSelectedRows();
                var selectedUser = [];
                jQuery.each(selectedIndexes, function (index, value) {
                    selectedUser.push(grid.getData()[value]["UserName"]);
                });
                deleteUser(selectedUser);
            });
            $("#ButtonDelete").click(function () {
                // grid.setSelectedRows(1);
                //grid.render();
                alert(grid.getSelectedRows());
                var selectedIndexes = grid.getSelectedRows();
                var selectedUser= [];
                jQuery.each(selectedIndexes, function (index, value) {
                    selectedUser.push(grid.getData()[value]["UserName"]);
                });
                deleteUser(selectedUser);
            });
        });

    </script>
    
    
    
    <script type="text/javascript">
        //$(document).ready(function() {
        FB.init({
            appId: '146719695433980', cookie: true,
            status: true, xfbml: true, oauth: true
        });

        //  console.log('after init');
        FB.getLoginStatus(function (response) {
            alert(response.status);
            if (response.status == 'connected') {
                //   console.log('connected');
                if (response.authResponse) {
                    alert(response.email);
                    //  console.log('Welcome!  Fetching your information.... ');
                    FB.api('/me', function (response) {
                        if ((response.email == null) || (response.email == "")) {
                            //        console.log('scope permission is not allowed ');
                            //location.href = "/minipoll/";
                        }
                        //  console.log('Good to see you, ' + response.name + response.email + '.');

                        fb.fb_user_id = response.id;
                        fb.fb_email = response.email;
                        fb.fb_name = response.first_name;
                        fb.fb_lastname = response.last_name;
                        //loadNextQuestion(fb.fb_user_id, "celeb");
                        //loadNextQuestion(fb.fb_user_id, "body");
                        startQuestion(fb.fb_user_id, "celeb");
                        startQuestion(fb.fb_user_id, "body");
                        //FB.logout(function(response) {
                        //  console.log('Logged out.');
                        //});
                    });
                }
                else {
                    //  console.log('no authResponse.');
                }
            }
            else if (response.status === 'not_authorized') {
                //   console.log('not authorized');
                location.href = "/minipoll";
            }
            else {
                // console.log('no facebook login');
                location.href = "/minipoll";
            }
        });    
               
        

     </script>
</asp:Content>
<asp:Content ID="NorthPaneContent" runat="server" ContentPlaceHolderID="NorthPaneContent">
    <div id="myGrid" style="width: 100%; height: 300px;">
    </div>
    <div id="dialog-form" class="ui-dialog-content ui-widget-content" style="width: auto;
        min-height: 0px; height: 208.4px;" scrolltop="0" scrollleft="0">
        <p class="validateTips">
            All form fields are required.</p>
        <form>
        <fieldset>
            <div style="clear: both">
            </div>
            <label for="name">
                Name</label>
            <br />
            <input type="text" class="text ui-widget-content ui-corner-all" id="name" name="name">
            <br />
            <label for="email">
                Email</label>
            <br />
            <input type="text" class="text ui-widget-content ui-corner-all" value="" id="email"
                name="email">
            <br />
            <label for="password">
                Password</label>
            <br />
            <input type="password" class="text ui-widget-content ui-corner-all" value="" id="password"
                name="password">
        </fieldset>
        </form>
    </div>
    <button id="create-user">
        Create new user</button>
    <button id="ButtonDelete">
        Edit</button>
</asp:Content>
